using System;
using System.Collections.Generic;
using System.Data;

namespace AutoTool
{
    public class UserFilesRepository
    {
        private readonly DatabaseHelper _dbHelper;

        public UserFilesRepository(string connectionString)
        {
            _dbHelper = new DatabaseHelper(connectionString);
        }

        public List<UserFiles> GetAllUserFiles()
        {
            string query = "SELECT * FROM UserFiles";
            DataTable result = _dbHelper.ExecuteQuery(query);
            return DataMapper.MapToList<UserFiles>(result);
        }

        public UserFiles GetUserFilesById(Guid id)
        {
            string query = "SELECT * FROM UserFiles WHERE FileId = @FileId";
            var parameters = new Dictionary<string, object>
            {
                { "@FileId", id }
            };

            DataTable result = _dbHelper.ExecuteQuery(query, parameters);
            if (result.Rows.Count > 0)
            {
                return DataMapper.MapToObject<UserFiles>(result.Rows[0]);
            }
            return null;
        }

        public void InsertUserFiles(UserFiles entity)
        {
            string query = "INSERT INTO UserFiles (FileId, UserId, FileName, FileSize, FilePath, UploadTime, IsDeleted, FileType) VALUES (@FileId, @UserId, @FileName, @FileSize, @FilePath, @UploadTime, @IsDeleted, @FileType)";
            var parameters = new Dictionary<string, object>
            {
                { "@FileId", entity.FileId },
                { "@UserId", entity.UserId },
                { "@FileName", entity.FileName },
                { "@FileSize", entity.FileSize },
                { "@FilePath", entity.FilePath },
                { "@UploadTime", entity.UploadTime },
                { "@IsDeleted", entity.IsDeleted },
                { "@FileType", entity.FileType }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }

        public void DeleteUserFiles(Guid id)
        {
            string query = "DELETE FROM UserFiles WHERE FileId = @FileId";
            var parameters = new Dictionary<string, object>
            {
                { "@FileId", id }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }
    }
}